#include <CTime>
#include <iostream>
#include <CMath>

#include "Data.h"
#include "Network.h"
#include "Normalizer.h"
#include "Preprocessing.h"

using namespace std;

int main(int argc, char* argv[])
{
	srand((int)time(NULL));
	/////////////////////////////////////////////////////////////
	Preprocessing ptrain;
	Preprocessing ptest;
    ptrain.readFile("data\\training.txt");
	ptest.readFile("data\\testing.txt");
	ptrain.setRowAndColunm(1000000000,5);
	ptest.setRowAndColunm(1000000000,5);
	cout << "read finish!" << endl;
	ptrain.dataClean();
	ptest.dataClean();
	cout << "clean finish!" << endl;
	//ptrain.output();
	ptrain.dataRecognize();
	ptrain.writeFile("data\\result.txt");
	ptest.dataRecognize();
	cout << "Recognize finish!" << endl;
	//ptrain.output();
	////////////////////////////////////////////////////////////
	//p.writeFile("data\\result.txt");
	 //==================================================
	 Network network(4,9, 1);
	// network.printNetwork();
	 //==================================================
	 //--------------------------------------------------
	 //Network network(3,2,1);
	 //--------------------------------------------------
	 network.setLearningRate(0.9);
	 //network.printNetwork();
	 //--------------------------------------------------
	 //double input[3] = {1.0, 0.0, 1.0};
	 //--------------------------------------------------
//		network.forword(input);
//		double[] output = {1.0};
//		network.backpropagation(output);
		
	/*input[0] = 1.0;
	input[1] = 0;
	input[2] =1;*/
	 //====================================
	//Data trainData("data\\training.txt");
	//=====================================
	Data trainData(ptrain.datasetRes);
	Normalizer normalizer;
//	normalizer.normalizeByMinMax(trainData.dataset, true);
	
	//cout << trainData.dataset.at(0).at(2) << " " << trainData.dataset.at(0).at(3) << " " <<endl;
	normalizer.normalizeByZScore(trainData.dataset, true);
	//int temp = (int)trainData.dataset.at(0).at(4);
	//cout << trainData.dataset.at(0).at(2) << " " << trainData.dataset.at(0).at(3) << " "<< temp <<endl;
	
	/*for(int i = 0; i < trainData.dataset.size() && i < 10; i++)
	{
		for(int j = 0 ;j < trainData.dataset.at(i).size();j++)
		{
			cout << trainData.dataset.at(i).at(j) << "<>";
		}
		cout << endl;
	}*/
	/*network.printNetwork();
	for(int i = 0; i < trainData.dataset.size() && i < 10; i++)
	{
		for(int j = 0 ;j < trainData.dataset.at(i).size();j++)
		{
			cout << trainData.dataset.at(i).at(j) << "--";
		}
		cout << endl;
	}*/
//	normalizer.normalizeByLogistic(trainData.dataset, true);
	cout << trainData.calMisRecord(network)<<endl;
	
	for (int long i= 0; i < 100000; i++){
		int index = abs(rand()) % trainData.dataset.size();
	//	int index = i % trainData.dataset.size();
		//===============================================================
		double* input = new double[trainData.attributeNum - 2];
		for (int j = 0; j < trainData.attributeNum - 2; j++){
			input[j] = trainData.dataset.at(index).at(j+2);
	//		cout << input[j] << " ";
		}
	//	cout << " : ";
		//===============================================================
		network.forword(input);
		//double* output = new double[1];
		double *output = network.getOutputFromNumber(trainData.dataset.at(index).at(1));
		//===============================================================
		//output[0] = trainData.dataset.at(index).at(1);
		//	cout << output[0] <<endl;
		//===============================================================
		//---------------------------------------------------------------
		//output[0] = 1;
		//---------------------------------------------------------------
		//cout << "output=" << output[0] <<endl;
		network.backpropagation(output);
		//network.printNetwork();
	}
	//network.printNetwork();
	cout << trainData.calMisRecord(network) << endl;
	
	//Data testData("data\\wine.data");
	Data testData(ptest.datasetRes);
	//Data testData("data\\testing.txt");
//	normalizer.normalizeByMinMax(testData.dataset, false);
//	cout << testData.dataset.at(0)[2] << " " << testData.dataset.at(0)[3] << " "<< testData.dataset.at(0)[4] <<endl;
	normalizer.normalizeByZScore(testData.dataset, false);
//	cout << testData.dataset.at(0)[2] << " " << testData.dataset.at(0)[3] << " "<< testData.dataset.at(0)[4] <<endl;
///	normalizer.normalizeByLogistic(testData.dataset, false);
	cout << testData.calMisRecord(network)<<endl;
//	cout << testData.calMisRecord(network)<<endl;
}